package com.foo.XMLWriter;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


import javax.ejb.Stateless;

/**
 * Session Bean implementation class XMLGenerationModule
 */
@Stateless
public class XMLGenerationModule implements XMLGenerationModuleRemote, XMLGenerationModuleLocal {

    /**
     * Default constructor. 
     */
    public XMLGenerationModule() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
	public void generateXMLFile(String xFileName ,String[] data, String type)
	{
    try {
    	if(xFileName.contains(".xml"))
    	{
    		xFileName=xFileName.replace(".xml", "");
    	}
    	
    	DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("StockData");
		doc.appendChild(rootElement);
 
 
		if(type.equals("Spatial"))
		{			
			int j=1;
				for(int i=0;i<data.length;i+=8)
			    {
				// Stock Record elements
				Element stock = doc.createElement("StockRecord");
				rootElement.appendChild(stock);
		 
				// set attribute to Stock element
				Attr attr = doc.createAttribute("id");
				attr.setValue(""+j);
				stock.setAttributeNode(attr);
		  
					// sub elements of stockRecord
					Element tickerSymbol = doc.createElement("TickerSymbol");
					tickerSymbol.appendChild(doc.createTextNode(data[i]));
					stock.appendChild(tickerSymbol);
			 
					Element company = doc.createElement("Company");
					company.appendChild(doc.createTextNode(data[i+1]));
					stock.appendChild(company);
			 
					Element sector = doc.createElement("Sector");
					sector.appendChild(doc.createTextNode(data[i+2]));
					stock.appendChild(sector);
					
					Element industry = doc.createElement("Industry");
					industry.appendChild(doc.createTextNode(data[i+3]));
					stock.appendChild(industry);
			 
					Element country = doc.createElement("Country");
					country.appendChild(doc.createTextNode(data[i+4]));
					stock.appendChild(country);
					
					Element state = doc.createElement("State");
					state.appendChild(doc.createTextNode(data[i+5]));
					stock.appendChild(state);
			
					Element marketcap = doc.createElement("MarketCap");
					marketcap.appendChild(doc.createTextNode(data[i+6]));
					stock.appendChild(marketcap);
			    }
		}
		else if(type.equals("Temporal"))
		{
			int j=1;
				for(int i=0;i<data.length;i+=7)
			    {
				// Stock Record elements
				Element stock = doc.createElement("StockRecord");
				rootElement.appendChild(stock);
		 
				// set attribute to Stock element
				Attr attr = doc.createAttribute("id");
				attr.setValue(""+j);
				stock.setAttributeNode(attr);
		  
					// sub elements of stockRecord
					Element tickerSymbol = doc.createElement("TickerSymbol");
					tickerSymbol.appendChild(doc.createTextNode(data[i]));
					stock.appendChild(tickerSymbol);
			 
					Element date_value = doc.createElement("Date");
					date_value.appendChild(doc.createTextNode(data[i+1]));
					stock.appendChild(date_value);
					
					Element open_value = doc.createElement("OpenValue");
					open_value.appendChild(doc.createTextNode(data[i+2]));
					stock.appendChild(open_value);
			 
					Element high_value = doc.createElement("HighValue");
					high_value.appendChild(doc.createTextNode(data[i+3]));
					stock.appendChild(high_value);
					
					Element low_value = doc.createElement("LowValue");
					low_value.appendChild(doc.createTextNode(data[i+4]));
					stock.appendChild(low_value);
			 
					Element close_value = doc.createElement("CloseValue");
					close_value.appendChild(doc.createTextNode(data[i+5]));
					stock.appendChild(close_value);
					
					Element volume = doc.createElement("Volume");
					volume.appendChild(doc.createTextNode(data[i+6]));
					stock.appendChild(volume);
			    }
		}
		else if(type.equals("Historical"))
		{
			int j=1;
			for(int i = 0; i < data.length; i+=7)
			{	
			// Stock Record elements
			Element stock = doc.createElement("StockRecord");
			rootElement.appendChild(stock);
	 
			// set attribute to Stock element
			Attr attr = doc.createAttribute("id");
			attr.setValue(""+j);
			stock.setAttributeNode(attr);
	  
				// sub elements of stockRecord
				Element date = doc.createElement("Date");
				date.appendChild(doc.createTextNode(data[i]));
				stock.appendChild(date);	
				
				// sub elements of stockRecord
				Element tickerSymbol = doc.createElement("TickerSymbol");
				tickerSymbol.appendChild(doc.createTextNode(data[i+1]));
				stock.appendChild(tickerSymbol);
		 
				Element open_value = doc.createElement("OpenValue");
				open_value.appendChild(doc.createTextNode(data[i+2]));
				stock.appendChild(open_value);
		 
				Element high_value = doc.createElement("HighValue");
				high_value.appendChild(doc.createTextNode(data[i+3]));
				stock.appendChild(high_value);
				
				Element low_value = doc.createElement("LowValue");
				low_value.appendChild(doc.createTextNode(data[i+4]));
				stock.appendChild(low_value);
		 
				Element close_value = doc.createElement("CloseValue");
				close_value.appendChild(doc.createTextNode(data[i+5]));
				stock.appendChild(close_value);
				
				Element volume = doc.createElement("Volume");
				volume.appendChild(doc.createTextNode(data[i+6]));
				stock.appendChild(volume);
				j++;
			}
		}
		if(type.equals("Snapshot"))
		{
			int j=1;
			for(int i = 0; i < data.length; i+=7)
			{	
			// Stock Record elements
			Element stock = doc.createElement("StockRecord");
			rootElement.appendChild(stock);
	 
			// set attribute to Stock element
			Attr attr = doc.createAttribute("id");
			attr.setValue(""+j);
			stock.setAttributeNode(attr);
	  
				// sub elements of stockRecord
				Element date = doc.createElement("Date");
				date.appendChild(doc.createTextNode(data[i]));
				stock.appendChild(date);	
				
				// sub elements of stockRecord
				Element tickerSymbol = doc.createElement("TickerSymbol");
				tickerSymbol.appendChild(doc.createTextNode(data[i+1]));
				stock.appendChild(tickerSymbol);
		 
				Element open_value = doc.createElement("OpenValue");
				open_value.appendChild(doc.createTextNode(data[i+2]));
				stock.appendChild(open_value);
		 
				Element high_value = doc.createElement("HighValue");
				high_value.appendChild(doc.createTextNode(data[i+3]));
				stock.appendChild(high_value);
				
				Element low_value = doc.createElement("LowValue");
				low_value.appendChild(doc.createTextNode(data[i+4]));
				stock.appendChild(low_value);
		 
				Element close_value = doc.createElement("CloseValue");
				close_value.appendChild(doc.createTextNode(data[i+5]));
				stock.appendChild(close_value);
				
				Element volume = doc.createElement("Volume");
				volume.appendChild(doc.createTextNode(data[i+6]));
				stock.appendChild(volume);
				j++;
			}
		}
		if(type.equals("Analyzed"))
		{
			int j =1;
			for(int i=0;i<data.length;i+=7)
		    {
			// Stock Record elements
			Element stock = doc.createElement("StockRecord");
			rootElement.appendChild(stock);
	 
			// set attribute to Stock element
			Attr attr = doc.createAttribute("id");
			attr.setValue(""+j);
			stock.setAttributeNode(attr);
	  
				// sub elements of stockRecord
				Element tickerSymbol = doc.createElement("TickerSymbol");
				tickerSymbol.appendChild(doc.createTextNode(data[i]));
				stock.appendChild(tickerSymbol);
		 
				Element open_value = doc.createElement("OpenValue");
				open_value.appendChild(doc.createTextNode(data[i+1]));
				stock.appendChild(open_value);
		 
				Element high_value = doc.createElement("HighValue");
				high_value.appendChild(doc.createTextNode(data[i+2]));
				stock.appendChild(high_value);
				
				Element low_value = doc.createElement("LowValue");
				low_value.appendChild(doc.createTextNode(data[i+3]));
				stock.appendChild(low_value);
		 
				Element close_value = doc.createElement("CloseValue");
				close_value.appendChild(doc.createTextNode(data[i+4]));
				stock.appendChild(close_value);
				
				Element volume = doc.createElement("Volume");
				volume.appendChild(doc.createTextNode(data[i+5]));
				stock.appendChild(volume);
				
				Element perc_change = doc.createElement("PercentageChange");
				perc_change.appendChild(doc.createTextNode(data[i+6]));
				stock.appendChild(perc_change);
		    }
		}
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File(xFileName+".xml"));
		 
				
		transformer.transform(source, result);
 
		System.out.println("XML File saved!");
 
	  } catch (ParserConfigurationException pce) {
		pce.printStackTrace();
	  } catch (TransformerException tfe) {
		tfe.printStackTrace();
	  }
	}

}
